//
//  ANLoginController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANLoginController.h"
#import "LoginTextField.h"
#import "ANGradientButton.h"
#import "ANFindPsdController.h"
#import "ANRegisterController.h"
#import "ANTabBarController.h"
#import <Bugly/Bugly.h>
#import "AppDelegate.h"

@interface ANLoginController ()

@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) LoginTextField *telTextField;

@property (nonatomic, strong) LoginTextField *psdTextField;

@property (nonatomic, strong) ANGradientButton *loginButton;

@property (nonatomic, strong) UIButton *forgetPsdBtn;

@property (nonatomic, strong) UILabel *registerLab;
@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation ANLoginController

- (void)closeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeAction)];
    
    [self initData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initData];
}

- (void)initData
{
    if (UserUtils.shared.telString.length) {
        self.telTextField.textField.text = UserUtils.shared.telString;
    }
}

- (void)requestData
{
    if (!self.telTextField.textField.text || self.telTextField.textField.text.length < 1) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    if (!self.psdTextField.textField.text || self.psdTextField.textField.text.length < 1) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    NSDictionary *para = @{
                           @"userName":self.telTextField.textField.text,
                           @"password":self.psdTextField.textField.text
                           };
    [MBProgressHUD showActivityMessage:@"正在登陆..."];
    [URLManager.shareManager postUrlStr:Api_User_Login parameters:para success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                [UserUtils.shared saveUserData:responseObj[@"data"]];
                [JPUSHService setAlias:UserUtils.shared.vipId completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                    NSLog(@"iResCode:%ld;iAlias:%@",(long)iResCode,iAlias);
                } seq:8898];
                AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                [delegate addJPCustomNotification];
                [self dismissViewControllerAnimated:YES completion:nil];
                [MBProgressHUD showSuccess:responseObj[@"message"]];
                [Bugly setUserIdentifier:UserUtils.shared.telString];
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}

- (void)setupSubViews
{
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.icon = [[UIImageView alloc] init];
    self.icon.image = [UIImage imageNamed:@"logo"];
    [self.scrollView addSubview:self.icon];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollView);
        make.top.equalTo(self.scrollView).offset(kWidth(60));
    }];
    
    self.telTextField = [[LoginTextField alloc] init];
    self.telTextField.type = LoginTextFieldLoginTel;
    [self.scrollView addSubview:self.telTextField];
    [self.telTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon.mas_bottom).offset(kWidth(50));
        make.centerX.equalTo(self.scrollView);
        make.left.equalTo(self.scrollView).offset(kWidth(15));
        make.right.equalTo(self.scrollView).offset(-kWidth(15));
        make.height.equalTo(@(kWidth(48)));
        make.width.equalTo(@(kScreenW-kWidth(30)));
    }];
    
    self.psdTextField = [[LoginTextField alloc] init];
    self.psdTextField.type = LoginTextFieldLoginPsd;
    [self.scrollView addSubview:self.psdTextField];
    [self.psdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.telTextField.mas_bottom).offset(kWidth(20));
        make.left.equalTo(self.scrollView).offset(kWidth(15));
        make.right.equalTo(self.scrollView).offset(-kWidth(15));
        make.height.equalTo(@(kWidth(48)));
    }];
    
    self.loginButton = [[ANGradientButton alloc] initWithFrame:CGRectMake(0, 0, kScreenW-kWidth(30), kWidth(48))];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.scrollView addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.psdTextField.mas_bottom).offset(kWidth(60));
        make.left.equalTo(self.scrollView).offset(kWidth(15));
        make.right.equalTo(self.scrollView).offset(-kWidth(15));
        make.height.equalTo(@(kWidth(48)));
        make.bottom.equalTo(self.scrollView).offset(-kWidth(40));
    }];
    self.loginButton.layer.cornerRadius = kWidth(24);
    self.loginButton.layer.masksToBounds = YES;
    
    self.forgetPsdBtn = [[UIButton alloc] init];
    [self.forgetPsdBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [self.forgetPsdBtn setTitleColor:kColor333333 forState:UIControlStateNormal];
    self.forgetPsdBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.scrollView addSubview:self.forgetPsdBtn];
    [self.forgetPsdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView).offset(30);
        make.top.equalTo(self.loginButton.mas_bottom).offset(15);
    }];
    
    self.registerLab = [[UILabel alloc] init];
    self.registerLab.textColor = kColor666666;
    self.registerLab.text = @"没有账号？";
    self.registerLab.font = [UIFont systemFontOfSize:13];
    [self.scrollView addSubview:self.registerLab];
    
    self.registerBtn = [[UIButton alloc] init];
    [self.registerBtn setTitleColor:UIColorFromRGB(0x2983FF) forState:UIControlStateNormal];
    [self.registerBtn setTitle:@"去注册" forState:UIControlStateNormal];
    self.registerBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.scrollView addSubview:self.registerBtn];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.scrollView).offset(-30);
        make.centerY.equalTo(self.forgetPsdBtn);
    }];
    
    [self.registerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.registerBtn.mas_left);
        make.centerY.equalTo(self.forgetPsdBtn);
    }];
    
    
    [self.forgetPsdBtn addTarget:self action:@selector(forgetAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton addTarget:self action:@selector(loginAciton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)forgetAction:(id)sender
{
    ANFindPsdController *vc = [[ANFindPsdController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)registerAction:(id)sender
{
    ANRegisterController *vc = [[ANRegisterController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)loginAciton:(id)sender
{
    [self requestData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
